<?php
/**
 * 菜单控制器
 */
class NodeAction extends CommonAction {

    private $levelArr=array('1'=>array('title'=>"项目",'key'=>"1"),'2'=>array('title'=>"模块",'key'=>"2"),'3'=>array('title'=>"方法",'key'=>"3"));
    private $allNode=array();
    private $tree=null;	

	public function _initialize()
	{
    $this->currentModel = D(MODULE_NAME);    
		parent::_initialize();
    import("@.ORG.Tree");
    $this->allNode=$this->currentModel->getAllNode();
    $this->tree=new Tree();		
		$this->assign('levelArr',$this->levelArr);		
	}

    public function index()
    {
      $newNode=$this->_treeData($this->allNode);
      $treeStr="<tr>
              <td>\$sort_order</td>
              <td>\$id</td>
              <td>\$spacer \$title</td>
              <td>\$name</td>
              <td>\$level</td>
              <td>\$is_show</td>
              <td>\$status</td>
              <td>
                \$add_child
                <a href='__URL__/edit/id/\$id' title='编辑' class='mr5'>编辑</a>
                <a href='__URL__/del/id/\$id' title'删除'>删除</a> 
              </td>
            </tr>";
      
      $this->tree->init($newNode);
      $treeStr=$this->tree->getTree(0,$treeStr,0);
      $this->assign('treeTrStr',$treeStr);      
      $this->display();
    }

    /**
      * 构造递归树需要的结构
      * @access private
      * @param array 原始数组
      * @return array 
      */
    private function _treeData($data)
    {
      $treeArr=array();
      foreach($data as $c)
      {
        $c['sort_order']="<input type='text' value='".$c['sort_order']."' name=sort[".$c['id']."]  class='text_input medium_input tc''/>";
        $c['status']=$c['status']==0?'禁止':'开放';
        $c['is_show']=$c['is_show']==0?'隐藏':'显示';
        if($c['level']!=3)
        {
        	$c['add_child']="<a href='__URL__/add/id/".$c['id']."' title='添加子菜单' class='mr5'>添加子菜单</a>";
        }
        $c['level']=$this->levelArr[$c['level']]['title'];
        $treeArr[$c['id']] = $c;
      }
      return $treeArr;
    }    

    public function add()
    {
    	if($this->isPost())
    	{
    		$this->_save();
    	}else
    		{
          		$parentId=$this->_get($this->pkId);
          		if(isset($parentId))
          		{
            		$parentData=$this->currentModel->field('title')->find($parentId);
            		if(empty($parentData))$this->error('参数违法',U(MODULE_NAME.'/index'));
          		}else
          			{
          				$parentId=0;
          			}
              	$this->tree->init($this->allNode);
          		$optionStr=$this->tree->getTree(0,"<option value=\$id \$selected>\$spacer\$title</option>",$parentId);
          		$this->assign('optionStr',$optionStr);       		
    			$this->display();
    		}
    }

    public function edit()
    {
    	if($this->isPost())
    	{
        	$funcName=$this->_post('actionType');
        	if(isset($funcName))
        	{
          		$this->$funcName();
        	}else
          		{
            		$this->_save();
          		}
    	}else
    		{
          		$id=$this->_get($this->pkId);
          		$data=$this->currentModel->find($id);
          		if(empty($data))$this->error('参数违法',U(MODULE_NAME.'/index'));
          		$this->tree->init($this->allNode);
          		$optionStr=$this->tree->getTree(0,"<option value=\$id \$selected>\$spacer\$title</option>",$data['parent_id']);
          		$this->assign('optionStr',$optionStr);         
          		$this->assign('data',$data);
    			$this->display('add');
    		}
    }

	public function del()
	{
		  $id=$this->_get($this->pkId);
      if($this->currentModel->where(array('parent_id'=>$id))->count()>0)
      {
        	$this->_delChild($id);
      }
      if($this->currentModel->delete($id)!=0)
      {	
        	$this->success('删除成功',U(MODULE_NAME.'/index'));
     	}
      else
        {$this->error('失败',U(MODULE_NAME.'/index'));}
	}

  private function _delChild($parentId)
  {
    $this->currentModel->where(array('parent_id'=>$parentId))->delete();
  }

    private function _save()
    {
    	if($this->currentModel->create()===FALSE)
    	{
    		$this->error($this->currentModel->getError());
    	}
     	$id=$this->_post($this->pkId);
      	$funcName=$id>0?'save':'add';
      	if($funcName=='save')
      	{
        	$this->_parentCorrect($_POST['parent_id'],$_POST['id']);
      	}      	
      	if($this->currentModel->$funcName()!==FALSE)
      	{
        	$this->success('成功',U(MODULE_NAME.'/index'));
      	}else
        	{
          		$this->error('失败');
          	}

    }

    /**
      * 栏目排序
      * @access private
      */
    private function _listOrder()
    {
      $orders=$_POST['sort'];
      foreach($orders as $k=>$v)
      {
        $map['id']=$k;
        $this->currentModel->where($map)->setField('sort_order',$v);
      }
      $this->success('操作成功',U(MODULE_NAME.'/index'));      
    }

    /**
      * 检查编辑菜单时上级菜单与本级菜单的冲突问题
      * 所选择的上级菜单不能是当前菜单或者当前菜单的子菜单
      * @access private
      * @param int $parentId
      * @param int $currentId
      */
    private function _parentCorrect($parentId,$currentId)
    {
      $this->tree->init($this->allNode);
      $child=$this->tree->getChildString($currentId).$currentId;
      if(in_array($parentId,explode(',',$child)))
      {
          $this->error("所选择的上级菜单不能是当前菜单或者当前菜单的下级菜单!");
      }
    }    

}
?>